package com.jdbc1234.common;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

/**
 * @author : 章仕烜
 * @create : 2019/12/19
 **/
public class ShiroUtils {
    public static Subject login(String configFile,
                                String userName,
                                String password){
        Factory<SecurityManager> securityManagerFactory =
                new IniSecurityManagerFactory(configFile);
        SecurityManager securityManager = securityManagerFactory
                .getInstance();
        SecurityUtils.setSecurityManager(securityManager);
        Subject currentUser = SecurityUtils.getSubject();
        UsernamePasswordToken token =
                new UsernamePasswordToken(userName,password);
        try {
            // 登录验证
            currentUser.login(token);
            System.err.println("登录验证成功!");
        } catch (AuthenticationException e) {
            e.printStackTrace();
            System.err.println("登录验证失败!");
        }
        return currentUser;
    }
}
